#ifndef _SEN_H_
#define _SEN_H_

#include "port.h"
#include <deque>
#include <stdlib.h>

class CCharReader;

class CharReaderIterator
{
public:
  CharReaderIterator(CCharReader *the_reader=NULL, int the_idx = 0)
    : idx(the_idx),  reader(the_reader) { }
  CharReaderIterator& operator++();     /* ++it */
  CharReaderIterator operator+(int i); /* *(it+i) */
  TWCHAR& operator*() const;    /* (*it) */
private:
  unsigned int idx;                      /* the index */
  CCharReader *reader;          
};


class CCharReader
{
  friend class CharReaderIterator;
public:
  typedef CharReaderIterator iterator;
  CCharReader(FILE *afp) : fp(afp), buf() { }
  iterator begin() {
    return iterator(this, 0);
  }
private:
  FILE *fp;
  std::deque<TWCHAR> buf;
};  

template <class iterator>
bool ReadSentence(wstring &wstr, iterator &first, bool bIngoreCRLF=false)
{
  wstr.clear();
  TWCHAR ch, chnext, ch2;
  int brk = 0;
  for (; (ch = *first) != WCH_NULL; ++first) {
    chnext = *(first+1);
    ch2 = *(first + 2);
    if ((ch == WCH_RETURN) && bIngoreCRLF && (chnext != WCH_RETURN))
      continue;
    if (ch == WCH_JUHAO || ch == WCH_WENHAO || ch == WCH_TANHAO ||
        ch == WCH_TANHAO || ch == WCH_SHENGLUEHAO)
      brk = 1;
    else if (ch == WCH_DOUHAO && chnext == WCH_DOUHAO)
      brk = 1;
    else if (ch == WCH_RETURN || ch == WCH_TAB)
      brk = 2;
    else if (ch == WCH_SPACE) {
      if (chnext == WCH_RETURN) {
        if (ch2 == WCH_SPACE || ch2 == WCH_TAB || ch2 == WCH_RETURN)
          brk = 2;
      } else if (chnext == WCH_SPACE || chnext == WCH_TAB)
        brk = 2;
    }
    if (brk != 0)
      break;
    wstr.push_back(ch);
  }
  if (brk == 2 && wstr.size() == 0) {
    do {
      wstr.push_back(*first);
      ch = *(++first);
    } while (ch == WCH_SPACE || ch == WCH_TAB || ch == WCH_RETURN);
  } else if ( brk == 1) {
    do {
      wstr.push_back(*first);
      ch = *(++first);
    } while (ch == WCH_JUHAO || ch == WCH_WENHAO || ch == WCH_TANHAO ||
             ch == WCH_FENHAO || ch == WCH_MAOHAO || ch == WCH_SHENGLUEHAO);
  }
  return (wstr.size() > 0);
} 
  
  
#endif /* _SEN_H_ */
